package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddLink;
import com.itheima.ydd.entity.YddTag;
import com.itheima.ydd.service.IYddLinkService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Slf4j
@RestController
@RequestMapping("/ydd_link")
public class YddLinkController {


    @Autowired
    private IYddLinkService yddLinkService;


    /**
     * Get请求  根据id删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delete(int id){
        log.info("根据id删除链接{}",id);
        yddLinkService.removeById(id);

        return ResultData.ok("删除成功",null);
    }


    /**
     * 添加链接参数
     * @param yddLink
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddLink yddLink){
        log.info("添加链接参数{}",yddLink);
        yddLinkService.save(yddLink);


        return ResultData.ok("添加成功",null);
    }


    /**
     * 链接修改参数
     * @param yddLink
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddLink yddLink){
        log.info("链接修改参数{}",yddLink);

        yddLinkService.updateById(yddLink);

        return ResultData.ok("修改完成",null);
    }


    /**
     * 链接详情数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(int id){
        log.info("链接详情数据回显id{}",id);

        LambdaQueryWrapper<YddLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddLink::getId,id);

        YddLink one = yddLinkService.getOne(queryWrapper);

        return ResultData.ok("",one);
    }


    /**
     * tag分页查询
     *
     * @param
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String type, int page, int limit) {
        log.info("内容管理/标签管理-分页查询 ...type ->{},page -> {}, limit -> {}", type, page, limit);
        Page pageInfo = new Page(page, limit);
        LambdaQueryWrapper<YddLink> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 按标签名称查询
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(type), YddLink::getType, type);

        yddLinkService.page(pageInfo, lambdaQueryWrapper);

        //分页数据封装到map集合
        Map pageMap = new HashMap();
        pageMap.put("count", pageInfo.getTotal());
        pageMap.put("totalPages", pageInfo.getPages());
        pageMap.put("pageSize", pageInfo.getSize());
        pageMap.put("currentPage", pageInfo.getCurrent());
        pageMap.put("data", pageInfo.getRecords());

        return ResultData.ok("", pageMap);
    }

}
